package com.meis.widget.radius;

import com.meis.widget.radius.delegate.RadiusCompoundButtonDelegate;
import ohos.agp.components.AbsButton;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.app.Context;

/**
 * @Author: AriesHoo on 2018/7/19 10:03
 * @E-Mail: AriesHoo@126.com
 * Function: 用于需要圆角矩形框背景的CheckBox的情况,减少直接使用CheckBox时引入的shape资源文件
 * Description:
 * 1、2018-2-5 14:27:16 初始化TextView的 RadiusTextDelegate
 */
public class RadiusCheckBox extends Checkbox implements Component.EstimateSizeListener, AbsButton.CheckedStateChangedListener {
    private RadiusCompoundButtonDelegate delegate;

    public RadiusCheckBox(Context context) {
        this(context, null);
    }

    public RadiusCheckBox(Context context, AttrSet attrs) {
        super(context, attrs);
        delegate = new RadiusCompoundButtonDelegate(this, context, attrs);
        setEstimateSizeListener(this::onEstimateSize);
        setCheckedStateChangedListener(this::onCheckedChanged);
        delegate.init();
    }

    /**
     * 获取代理类用于Java代码控制shape属性
     *
     * @return RadiusCompoundButtonDelegate
     */
    public RadiusCompoundButtonDelegate getDelegate() {
        return delegate;
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        if (delegate == null) {
            return false;
        }
        setEstimatedSize(EstimateSpec.getSize(i), EstimateSpec.getSize(i1));
        if (delegate.getWidthHeightEqualEnable() && getEstimatedWidth() > 0 && getEstimatedHeight() > 0) {
            int max = Math.max(getEstimatedWidth(), getEstimatedHeight());
            int measureSpec = EstimateSpec.getSizeWithMode(max, EstimateSpec.PRECISE);
            super.estimateSize(measureSpec, measureSpec);
        }
        if (delegate.getRadiusHalfHeightEnable()) {
            delegate.setRadius(getEstimatedHeight() / (float) 2);
        }
        return true;
    }


/*
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (delegate != null) {
            if (delegate.getRadiusHalfHeightEnable()) {
                delegate.setRadius(getHeight() / 2);
            }
            delegate.init();
        }
    }*/

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (delegate != null) {
            delegate.setSelected(selected);
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (delegate != null) {
            delegate.init();
        }
    }

    @Override
    public void setPressState(boolean pressState) {
        super.setPressState(pressState);
        if (delegate != null) {
            delegate.init();
        }
    }


    @Override
    public void setChecked(boolean checked) {
        super.setChecked(checked);
        if (delegate != null) {
            delegate.init();
        }
    }


    @Override
    public void onCheckedChanged(AbsButton absButton, boolean b) {
        setChecked(b);
    }
}
